﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace SpiderHelper.Tools.Sina
{
    /// <summary>
    /// 网络路径内容解析
    /// </summary>
    public class SinaURLAnalysis
    {
        public string AnalysisURLContent(string url, string encode, int channelId, out bool enSured)
        {
            string result = "";
            System.Net.CookieContainer container = new System.Net.CookieContainer();
            Common.Net.Request request = new Common.Net.Request(container);
            string html = "";
            try
            {
                html = request.ReadUrlContent(url, encode);
            }
            catch (Exception)
            {
                html = "";
            }

            int pageIndex = html.IndexOf("<p class='page'>");
            string docType = "未知的文章类型   ";
            if (pageIndex < 0)//没有分页的内容，不然用起来麻烦
            {

                bool changed = false;




                result = GetPicNews(html, out changed);
                if (!changed)
                {
                    result = GetNews(html, out changed);
                    docType = "时政新闻";
                }
                if (!changed)
                {
                    result = GetCars(html, out changed);
                    docType = "汽车等";
                }
                if (!changed)
                {
                    result = GetTuZuNews(html, out changed);
                    docType = "图组新闻";
                }
                if (!changed)
                {
                    result = GetWenHuaDoc(html, out changed);
                    docType = "文化，博客文章";
                }
                //System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"(?is)</?a\b[^>]*>(?!<img :(?!</?a).)*</a>");
                //result = reg.Replace(result, "");
                int tableIndex = result.IndexOf("<table STYLE=\"WiDTH: 480px; HeiGHT: 66px\" BORDER=\"8\"");
                if (tableIndex > 0)
                {
                    result = result.Substring(0, tableIndex);
                }
                result = result.Replace("onmouseover", "a");
                ("频道  " + channelId + "   " + docType + "    " + url).Debug();
            }
            else
            {
                ("频道  " + channelId + "   带分页的页面    " + url).Debug();
            }

            string images = GetImages(result);
            if (images.IndexOf("<img") < 0)
            {
                images = "";
            }
            string htmlstring = CommonTool.GetNoHTMLText(GetParagraph(result));
            if (htmlstring.IndexOf(images) >= 0)
            {
                result = htmlstring;
            }
            else
            {
                result = images + htmlstring;
            }


            if (!docType.Equals("文化，博客文章"))
            {
                enSured = true;
            }
            else
            {
                enSured = false;
            }

            if (result.ToLower().IndexOf("<script") < 0)
            {
                return result;
            }
            else
            {
                return "";
            }
        }


        private string GetImages(string docString)
        {
            //   docString = docString.ToLower();
            string images = "";
            int idx = docString.IndexOf("<img ");
            int idx2 = docString.IndexOf("<p>");//如果这个HTML是包含段落的，则处理，否则不处理，因为博客啥的分为有段落和没有段落的，很麻烦
            if (idx2 >= 0)
            {
                while (idx >= 0)
                {
                    docString = docString.Substring(idx);
                    int endIdx = docString.IndexOf(">");
                    images += docString.Substring(0, endIdx + 1);
                    docString = docString.Substring(endIdx + 1);
                    idx = docString.IndexOf("<img ");
                }
            }
            else
            {
                images = "";
            }
            return images.Trim();
        }


        /// <summary>
        /// 获取段落
        /// </summary>
        /// <param name="docString"></param>
        /// <returns></returns>
        private string GetParagraph(string docString)
        {
            string temp = "";

            docString = Regex.Replace(docString, @"<table[\s\S]*?</table>", "", RegexOptions.IgnoreCase);//去掉内容里面的Table
            string _docString = docString.ToLower();

            string paragraph = "";
            int idx = _docString.IndexOf("<p>");
            if (idx >= 0)
            {
                while (idx >= 0)
                {
                    docString = docString.Substring(idx);
                    _docString = _docString.Substring(idx);

                    int endIdx = _docString.IndexOf("</p>");
                    paragraph += docString.Substring(0, endIdx + 4);

                    docString = docString.Substring(endIdx + 4);
                    _docString = _docString.Substring(endIdx + 4);

                    idx = _docString.IndexOf("<p>");
                }
                idx = paragraph.IndexOf("<script");//现在的段落还包含脚本

                while (idx >= 0)
                {
                    temp += paragraph.Substring(0, idx);
                    int endIdx = paragraph.IndexOf("</script>");
                    paragraph = paragraph.Substring(endIdx + 9);
                    idx = paragraph.IndexOf("<script");//现在的段落还包含脚本
                }
                temp += paragraph;
            }
            else
            {
                temp = docString;
            }

            return temp.Trim();
        }

        /// <summary>
        /// 文化，博客文章
        /// </summary>
        /// <param name="docString"></param>
        /// <param name="docType"></param>
        /// <returns></returns>
        private string GetWenHuaDoc(string docString, out bool changed)
        {
            int blogindex = docString.IndexOf("<div id=\"sina_keyword_ad_area2\" class=\"articalContent  \">");
            if (blogindex > 0)//文化，博客文章？
            {
                docString = docString.Substring(blogindex);
                int endIndex = docString.IndexOf("</div>") + 6;
                docString = docString.Substring(0, endIndex).Trim();
                if (docString.IndexOf("real_src") > 0)
                {
                    docString = docString.Replace("src=\"http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif\"", "");
                    docString = docString.Replace("real_src", "src");
                }
                changed = true;
            }
            else
            {
                changed = false;
            }
            return docString;
        }
        /// <summary>
        /// 普通图片新闻
        /// </summary>
        /// <param name="docString"></param>
        /// <returns></returns>
        private string GetPicNews(string docString, out bool changed)
        {
            int idx = docString.IndexOf("<div class=\"blkContainerSblkCon\" id=\"artibody\">");
            if (idx > 0)//普通图片新闻
            {


                docString = docString.Substring(idx);
                int imgIndex = docString.IndexOf("<div class=\"img_wrapper\">");
                string imgsrc = "";
                if (imgIndex >= 0)
                {
                    imgsrc = docString;
                    imgsrc = imgsrc.Substring(imgsrc.IndexOf("<div class=\"img_wrapper\">") + 25);
                    imgsrc = imgsrc.Substring(0, imgsrc.IndexOf("<span"));
                    //   imgsrc.Debug();
                }
                //分享控件1
                int fenXiangIndex = docString.IndexOf("<div  style=\"margin-right: 0px; margin-left: 0pt; padding-right: 0px;\" class=\"blkComment otherContent_01\">");
                //分享控件2
                int fenXiangIndex2 = docString.IndexOf("<div class=\"blkComment otherContent_01\" id=\"sinashareto\" style=\"margin-right:0px;margin-left:0;padding-right:0px;\">");
                if (fenXiangIndex > 0)
                {
                    docString = docString.Substring(0, fenXiangIndex);
                    docString += "<!-- publish_helper_end -->";
                }
                else if (fenXiangIndex2 > 0)
                {
                    docString = docString.Substring(0, fenXiangIndex2);
                    docString += "<!-- publish_helper_end -->";
                }
                else
                {
                    int endIndex = docString.IndexOf("<!-- publish_helper_end -->");
                    if (endIndex == -1)
                    {
                        endIndex = docString.IndexOf("<!-- 正文内容 end -->");
                        docString = docString.Substring(0, endIndex + 17);
                    }
                    else
                    {
                        docString = docString.Substring(0, endIndex + 27);
                    }
                    docString = docString.Substring(0, endIndex + 27);
                    int xiangguanIndex = docString.IndexOf("<strong>相关</strong>");
                    if (xiangguanIndex >= 0)
                    {
                        docString = docString.Substring(0, docString.IndexOf("<strong>相关</strong>"));
                        docString += "<!-- publish_helper_end -->";
                    }
                    docString += "</div>";
                }
                docString = docString.Trim();
                changed = true;
            }
            else
            {
                changed = false;
            }
            return docString;
        }

        /// <summary>
        /// 图组新闻
        /// </summary>
        /// <param name="docString"></param>
        /// <returns></returns>
        private string GetTuZuNews(string docString, out bool changed)
        {
            int imagelist = docString.IndexOf("<div id=\"eData\" style=\"display:none;\">");
            if (imagelist > 0)//图组新闻
            {
                docString = docString.Substring(imagelist);
                int endIndex = docString.IndexOf("</div>");
                docString = docString.Substring(0, endIndex + 6);
                docString = docString.Replace("style=\"display:none;\"", "").Trim();
                string images = "";

                while (docString.IndexOf("<dl>") > 0)
                {
                    int imgIndex = docString.IndexOf("<dd>");
                    docString = docString.Substring(imgIndex + 4);
                    int imgendIndex = docString.IndexOf("</dd>");
                    images += "<img src=\"" + docString.Substring(0, imgendIndex) + "\" /><br/>";
                    int dlindex = docString.IndexOf("</dl>");
                    docString = docString.Substring(dlindex);
                }
                docString = images;
                changed = true;
            }
            else
            {
                changed = false;
            }
            return docString;
        }

        /// <summary>
        /// 汽车等
        /// </summary>
        /// <param name="docString"></param>
        /// <returns></returns>
        private string GetCars(string docString, out bool changed)
        {
            int idx3 = docString.IndexOf("<div class=\"moduleParagraph\">");
            if (idx3 > 0)//汽车等。。。
            {
                docString = docString.Substring(idx3);
                //分享控件1
                int fenXiangIndex = docString.IndexOf("<div  style=\"margin-right: 0px; margin-left: 0pt; padding-right: 0px;\" class=\"blkComment otherContent_01\">");
                //分享控件2
                int fenXiangIndex2 = docString.IndexOf("<div class=\"blkComment otherContent_01\" id=\"sinashareto\" style=\"margin-right:0px;margin-left:0;padding-right:0px;\">");
                if (fenXiangIndex > 0)
                {
                    docString = docString.Substring(0, fenXiangIndex);
                    docString += "<!-- publish_helper_end -->";
                }
                else if (fenXiangIndex2 > 0)
                {
                    docString = docString.Substring(0, fenXiangIndex2);
                    docString += "<!-- publish_helper_end -->";
                }
                else
                {
                    int endIndex = docString.IndexOf("<!-- publish_helper_end -->");
                    if (endIndex == -1)
                    {
                        endIndex = docString.IndexOf("<!-- 正文内容 end -->");
                        docString = docString.Substring(0, endIndex + 17);
                    }
                    else
                    {
                        docString = docString.Substring(0, endIndex + 27);
                    }
                    if (endIndex == -1)
                    {
                        changed = false;
                        return "";
                    }
                    docString = docString.Substring(0, endIndex + 27);
                    int xiangguanIndex = docString.IndexOf("<div id=\"news_like\" class=\"news_likedbox_style4\"></div>");
                    if (xiangguanIndex >= 0)
                    {
                        docString = docString.Substring(0, docString.IndexOf("<div id=\"news_like\" class=\"news_likedbox_style4\"></div>"));
                        docString += "<!-- publish_helper_end -->";
                    }
                    docString += "</div>";
                }
                docString = docString.Trim();
                changed = true;
            }
            else
            {
                changed = false;
            }
            return docString;
        }
        /// <summary>
        /// 时政新闻
        /// </summary>
        /// <param name="docString"></param>
        /// <returns></returns>
        private string GetNews(string docString, out bool changed)
        {
            int idx2 = docString.IndexOf("<div class=\"blkContainerSblkCon BSHARE_POP\" id=\"artibody\">");
            if (idx2 == -1)
            {
                idx2 = docString.IndexOf("<div class=\"BSHARE_POP blkContainerSblkCon\" id=\"artibody\">");
            }
            if (idx2 > 0)//时政新闻
            {
                docString = docString.Substring(idx2);
                //分享控件1
                int fenXiangIndex = docString.IndexOf("<div  style=\"margin-right: 0px; margin-left: 0pt; padding-right: 0px;\" class=\"blkComment otherContent_01\">");
                //分享控件2
                int fenXiangIndex2 = docString.IndexOf("<div class=\"blkComment otherContent_01\" id=\"sinashareto\" style=\"margin-right:0px;margin-left:0;padding-right:0px;\">");
                if (fenXiangIndex > 0)
                {
                    docString = docString.Substring(0, fenXiangIndex);
                    docString += "<!-- publish_helper_end -->";
                }
                else if (fenXiangIndex2 > 0)
                {
                    docString = docString.Substring(0, fenXiangIndex2);
                    docString += "<!-- publish_helper_end -->";
                }
                else
                {
                    int endIndex = docString.IndexOf("<!-- publish_helper_end -->");
                    if (endIndex == -1)
                    {
                        endIndex = docString.IndexOf("<!-- 正文内容 end -->");
                        docString = docString.Substring(0, endIndex + 17);
                    }
                    else
                    {
                        docString = docString.Substring(0, endIndex + 27);
                    }

                    int xiangguanIndex = docString.IndexOf("<strong>相关</strong>");
                    if (xiangguanIndex >= 0)
                    {
                        docString = docString.Substring(0, docString.IndexOf("<strong>相关</strong>"));
                        docString += "<!-- publish_helper_end -->";
                    }
                    docString += "</div>";
                }
                docString = docString.Trim();
                changed = true;
            }
            else
            {
                changed = false;
            }
            return docString;
        }
    }



}



